const express=require('express')
const session=require('express-session')
const app=new express()
app.use(express.json())
app.use(express.urlencoded({extended:false}))
app.use(session({
	secret:"zhouzong",//任意字符串
	resave:false,//固定写法
	saveUninitialized:true//固定写法
}))
app.post('/api/login',function(req,rep) {
	if( !(req.body.username=='admin' && req.body.password=='123456')) {
		return rep.send({
			status:500,
			message:'登录失败'
		})
	}
	//用户信息保存到session当中
	req.session.user=req.body
	req.session.isLogin=true
	rep.send({
		body:req.body,
		message:'登录成功'
	})
})
//获取session中的信息
app.get('/api/user',function(req,rep) {
	if(req.session.isLogin) {
		return rep.send({
			info:req.session.user
		})
	}
	rep.send({
		message:'请先登录'
	})
	
})
//退出登录 清空session 中信息 req.session.detroy
app.listen(3000,function() {
	console.log('启动成功')
})